<?php
    include_once '../Core/Conexion.php';
    $conexion=new Conexion("localhost","cemsa","root","1234");
    $conexion->conexion();
    $lista=$conexion->consultaLibre("SELECT * FROM ".$_REQUEST['tabla']."");
    
    
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Buscar</title>
</head>
<body>

<script>
	function enviar()
	{
		opener.document.form1.<?php echo $_REQUEST['campo'] ?>.value=document.getElementById('txtenviar').value;
		self.close();
	}
	
		
	
</script>

<div align="center">
    <table width="200" border="0">
      <tr>
        <td><input name="txtenviar" id="txtenviar" type="text" /></td>
        <td><input onclick="enviar();" name="" style="background-color:#009;color:#FFF" type="button" value="E n v i a r" /></td>
      </tr>
      <tr>
        <td><input name="txtbuscar" id="txtbuscar" type="text" /></td>
        <td><input onclick="buscar();" name="" style="background-color:#F60;color:#FFF" type="button" value="B u s c a r" /></td>
      </tr>
    </table>
      
      <select name="lista" id="lista" size="10" onchange="asignar();">
           <optgroup label='<?php echo 'Lista de '.$_REQUEST['tabla']  ?>' >
               <?php
               $fila= mysql_fetch_array($lista);
               
               $letra="";
               $datos;
               $cont=0;
               do
               {
                   
                   $letra=$letra."<option>";
                   $cadena="";
                   for ($i = 0; $i < $_REQUEST['tam']; $i++) 
                   {
                       $cadena=$cadena.$fila[$i]." | "; 
                   }
                   $letra=$letra.$cadena."</option>";
                   $datos[$cont]=$cadena;
                   $cont++;
                   
               }while($fila=  mysql_fetch_array($lista));
               
               echo $letra;     
               ?>
           </optgroup>    
      </select>
    
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
<script>
        function buscar()
            {
                    var lista=document.getElementById('lista');
                    var txtbuscar=document.getElementById('txtbuscar').value;
                   
                    //nme permite borrar todos los valores para escribir

                    for(i=(lista.length-1); i>=0; i--)
                    {
                            borrar = lista.options[i];
                            borrar.parentNode.removeChild(borrar);
                    }

                    <?php
                       for ($index = 0; $index < count($datos); $index++) 
                       {
                    ?>
                        
                        if("<?php  echo $datos[$index]; ?>".indexOf(txtbuscar)>=0)
			{
                            //alert(txtbuscar);
                            option = document.createElement("OPTION");
                            option.text = "<?php  echo $datos[$index]; ?>";
                            lista.add(option);
                        }
                    <?php
                       }
                    ?>
                   


            }
			
	function asignar()
	{
		var sel= document.getElementById("lista").value;
		//alert('asdasd');
		document.getElementById("txtenviar").value=sel.split("|")[0];
		
	}
	
</script>


</html>